package com.magold.game.factory;

import com.badlogic.gdx.math.Vector2;
import com.magold.algorithm.MagoldMath;
import com.magold.debug.EntityInfoPrinter;
import com.magold.game.level.MagoldLevel;
import com.magold.io.XMLReader;
import com.magold.io.helper.ReaderFacade;
import com.magold.level.ILevel;
import com.magold.logic.ISpriteLogic;
import com.magold.logic.magold.Magold;
import com.magold.sound.SoundNotifier;

public class MagoldFactory extends BaseLogicFactory {
	private ILevel mCachedLevel;
	
	@Override
	public boolean onLoadEntity(XMLReader pReader, ILevel pLoadingLevel) {
		mCachedLevel = pLoadingLevel;
		return super.onLoadEntity(pReader, pLoadingLevel);
	}

	public MagoldFactory() {
		super(Magold.class.getSimpleName());
	}

	@Override
	protected ISpriteLogic createSafely(XMLReader pReader) {
		Magold magold = new Magold();
		
		mCachedLevel.setAttributes(MagoldLevel.MAGOLD_KEY, magold);
		return magold;
	}

	@Override
	protected void onLoaded(ISpriteLogic pProduct, XMLReader pReader, ILevel pLevel) {
		Magold magold = (Magold) pProduct;
		
		magold.enableCastMagnetSkill(pLevel, pLevel.getPhysicsWorld());
		magold.enableSprintSkill();
		magold.enableSuppressSkill();
		Vector2 v = ReaderFacade.getVector2(pReader, "velocity");
		magold.applyVelocity(MagoldMath.pixel2Meter(v));
		
		float pInitPower = pReader.getFloat("power", 0);
		magold.addPower(pInitPower);
		
		pLevel.setSoundNotifier(new SoundNotifier(magold.getSprite()));
		
		magold.getSprite().registerUpdateHandler(new EntityInfoPrinter(magold.getSprite()));
	}

}
